

package com.maoshi.shop.api.listener;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.maoshi.shop.bean.app.dto.ShopCartItemDiscountDto;
import com.maoshi.shop.bean.app.dto.ShopCartItemDto;
import com.maoshi.shop.bean.app.dto.ShopCartOrderDto;
import com.maoshi.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.maoshi.shop.bean.enums.OrderStatus;
import com.maoshi.shop.bean.enums.OrderType;
import com.maoshi.shop.bean.event.SubmitOrderEvent;
import com.maoshi.shop.bean.model.*;
import com.maoshi.shop.bean.order.SubmitOrderOrder;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.dao.*;
import com.maoshi.shop.security.api.util.SecurityUtils;
import com.maoshi.shop.service.ProductService;
import com.maoshi.shop.service.SkuService;
import com.maoshi.shop.service.UserAddrOrderService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 确认订单信息时的默认操作
 *
 * @author LGH
 */
@Component("defaultSubmitOrderListener")
@AllArgsConstructor
public class SubmitOrderListener {


    private final MapperFacade mapperFacade;

    private final UserAddrOrderService userAddrOrderService;

    private final ProductService productService;

    private final SkuService skuService;

    private final Snowflake snowflake;

    private final OrderItemMapper orderItemMapper;

    private final SkuMapper skuMapper;

    private final ProductMapper productMapper;

    private final OrderMapper orderMapper;

    private final OrderSettlementMapper orderSettlementMapper;

    private final BasketMapper basketMapper;

    /**
     * 计算订单金额
     */
    @EventListener(SubmitOrderEvent.class)
    @Order(SubmitOrderOrder.DEFAULT)
    public void defaultSubmitOrderListener(SubmitOrderEvent event) {
        Date now = new Date();
        String userId = SecurityUtils.getUser().getUserId();

        ShopCartOrderMergerDto mergerOrder = event.getMergerOrder();

        // 订单商品参数
        List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders();

        List<Long> basketIds = new ArrayList<>();
        // 商品skuid为key 需要更新的sku为value的map
        Map<Long, Sku> skuStocksMap = new HashMap<>(16);
        // 商品productid为key 需要更新的product为value的map
        Map<Long, Product> prodStocksMap = new HashMap<>(16);

        // 把订单地址保存到数据库
        UserAddrOrder userAddrOrder = mapperFacade.map(mergerOrder.getUserAddr(), UserAddrOrder.class);
        if (userAddrOrder == null) {
            throw new MaoshiShopBindException("请填写收货地址");
        }
        userAddrOrder.setUserId(userId);
        userAddrOrder.setCreateTime(now);
        userAddrOrderService.save(userAddrOrder);

        // 订单地址id
        Long addrOrderId = userAddrOrder.getAddrOrderId();

        if(CollectionUtils.isNotEmpty(shopCartOrders)) {
            // 每个店铺生成一个订单
            for (ShopCartOrderDto shopCartOrderDto : shopCartOrders) {
                Double sumPlatformAmount = 0.0;
                // 使用雪花算法生成的订单号
                String orderNumber = String.valueOf(snowflake.nextId());
                shopCartOrderDto.setOrderNumber(orderNumber);

                Long shopId = shopCartOrderDto.getShopId();

                // 订单商品名称
                StringBuilder orderProdName = new StringBuilder(100);

                List<OrderItem> orderItems = new ArrayList<>();

                List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCartOrderDto.getShopCartItemDiscounts();
                for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) {
                    List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems();
                    for (ShopCartItemDto shopCartItem : shopCartItems) {
                        Sku sku = checkAndGetSku(shopCartItem.getSkuId(), shopCartItem, skuStocksMap);
                        Product product = checkAndGetProd(shopCartItem.getProdId(), shopCartItem, prodStocksMap);
                        OrderItem orderItem = new OrderItem();
                        orderItem.setShopId(shopId);
                        orderItem.setOrderNumber(orderNumber);
                        orderItem.setSkuId(sku.getSkuId());
                        orderItem.setProdId(sku.getProdId());
                        orderItem.setSkuName(sku.getSkuName());
                        orderItem.setProdCount(shopCartItem.getProdCount());
                        orderItem.setProdName(sku.getProdName());
                        orderItem.setPic(StrUtil.isBlank(sku.getPic()) ? product.getPic() : sku.getPic());
                        orderItem.setPrice(shopCartItem.getPrice());
                        orderItem.setUserId(userId);
                        orderItem.setProductTotalAmount(shopCartItem.getProductTotalAmount());
                        orderItem.setRecTime(now);
                        orderItem.setCommSts(0);
                        orderItem.setBasketDate(shopCartItem.getBasketDate());
                        //平台的补贴优惠金额
                        orderItem.setPlatformShareReduce(shopCartItem.getPlatformShareReduce());
                        // 实际订单项支付金额
                        orderItem.setActualTotal(shopCartItem.getActualTotal());
                        // 分摊优惠金额
                        orderItem.setShareReduce(shopCartItem.getShareReduce());
                        orderProdName.append(orderItem.getProdName()).append(",");
                        //推广员卡号
                        orderItem.setDistributionCardNo(shopCartItem.getDistributionCardNo());
                        //使用积分价格
                        orderItem.setUseScore(shopCartItem.getScorePrice());
                        orderItems.add(orderItem);

                        if (shopCartItem.getBasketId() != null && shopCartItem.getBasketId() != 0) {
                            basketIds.add(shopCartItem.getBasketId());
                        }
                    }

                }


                orderProdName.subSequence(0, Math.min(orderProdName.length() - 1, 100));
                if (orderProdName.lastIndexOf(",") == orderProdName.length() - 1) {
                    orderProdName.deleteCharAt(orderProdName.length() - 1);
                }


                // 订单信息
                com.maoshi.shop.bean.model.Order order = new com.maoshi.shop.bean.model.Order();

                order.setShopId(shopId);
                order.setOrderNumber(orderNumber);
                // 订单商品名称
                order.setProdName(orderProdName.toString());
                // 用户id
                order.setUserId(userId);
                // 商品总额
                order.setTotal(shopCartOrderDto.getTotal());
                // 实际总额
                order.setActualTotal(shopCartOrderDto.getActualTotal());
                order.setStatus(OrderStatus.UNPAY.value());
                order.setUpdateTime(now);
                order.setCreateTime(now);
                order.setIsPayed(0);
                order.setDeleteStatus(0);
                order.setProductNums(shopCartOrderDto.getTotalCount());
                order.setAddrOrderId(addrOrderId);
                order.setReduceAmount(shopCartOrderDto.getShopReduce());
                order.setFreightAmount(shopCartOrderDto.getTransfee());
                order.setRemarks(shopCartOrderDto.getRemarks());
                order.setOrderType(OrderType.ORDINARY.value());
                order.setPlatformAmount(shopCartOrderDto.getPlatformAmount());
                order.setOrderItems(orderItems);
                order.setScore(shopCartOrderDto.getUseScore());
                event.getOrders().add(order);
                // 插入订单结算表
                OrderSettlement orderSettlement = new OrderSettlement();
                orderSettlement.setUserId(userId);
                orderSettlement.setIsClearing(0);
                orderSettlement.setCreateTime(now);
                orderSettlement.setOrderNumber(orderNumber);
                orderSettlement.setPayAmount(order.getActualTotal());
                orderSettlement.setPayStatus(0);
                orderSettlement.setVersion(0);
                orderSettlement.setPayScore(0);
                //如果用使用积分，结算表将积分价格插入
                if(mergerOrder.getIsScorePay() != null && mergerOrder.getIsScorePay() == 1){
                    orderSettlement.setPayScore(shopCartOrderDto.getUseScore());
                }
                orderSettlementMapper.insert(orderSettlement);
            }
        }

        // 删除购物车的商品信息
        if (!basketIds.isEmpty()) {
            basketMapper.deleteShopCartItemsByBasketIds(userId, basketIds);
        }


        // 更新sku库存
        skuStocksMap.forEach((key, sku) -> {

            if (skuMapper.updateStocks(sku) == 0) {
                skuService.removeSkuCacheBySkuId(key, sku.getProdId());
                productService.removeProductCacheByProdId(sku.getProdId());
                throw new MaoshiShopBindException("商品：[" + sku.getProdName() + "]库存不足");
            }
        });

        // 更新商品库存
        prodStocksMap.forEach((prodId, prod) -> {

            if (productMapper.updateStocks(prod) == 0) {
                productService.removeProductCacheByProdId(prodId);
                throw new MaoshiShopBindException("商品：[" + prod.getProdName() + "]库存不足");
            }
        });

    }

    @SuppressWarnings({"Duplicates"})
    private Product checkAndGetProd(Long prodId, ShopCartItemDto shopCartItem, Map<Long, Product> prodStocksMap) {
        Product product = productService.getProductByProdId(prodId);
        if (product == null) {
            throw new MaoshiShopBindException("购物车包含无法识别的商品");
        }

        if (product.getStatus() != 1) {
            throw new MaoshiShopBindException("商品[" + product.getProdName() + "]已下架");
        }

        // 商品需要改变的库存
        Product mapProduct = prodStocksMap.get(prodId);

        if (mapProduct == null) {
            mapProduct = new Product();
            mapProduct.setTotalStocks(0);
            mapProduct.setProdId(prodId);
            mapProduct.setProdName(product.getProdName());

        }

        if (product.getTotalStocks() != -1) {
            mapProduct.setTotalStocks(mapProduct.getTotalStocks() + shopCartItem.getProdCount());
            prodStocksMap.put(product.getProdId(), mapProduct);
        }

        // -1为无限库存
        if (product.getTotalStocks() != -1 && mapProduct.getTotalStocks() > product.getTotalStocks()) {
            throw new MaoshiShopBindException("商品：[" + product.getProdName() + "]库存不足");
        }

        return product;
    }

    @SuppressWarnings({"Duplicates"})
    private Sku checkAndGetSku(Long skuId, ShopCartItemDto shopCartItem, Map<Long, Sku> skuStocksMap) {
        // 获取sku信息
        Sku sku = skuService.getSkuBySkuId(skuId);
        if (sku == null) {
            throw new MaoshiShopBindException("购物车包含无法识别的商品");
        }

        if (sku.getStatus() != 1) {
            throw new MaoshiShopBindException("商品[" + sku.getProdName() + "]已下架");
        }
        // -1为无限库存
        if (sku.getStocks() != -1 && shopCartItem.getProdCount() > sku.getStocks()) {
            throw new MaoshiShopBindException("商品：[" + sku.getProdName() + "]库存不足");
        }

        if (sku.getStocks() != -1) {
            Sku mapSku = new Sku();
            mapSku.setProdId(sku.getProdId());
            // 这里的库存是改变的库存
            mapSku.setStocks(shopCartItem.getProdCount());
            mapSku.setSkuId(sku.getSkuId());
            mapSku.setProdName(sku.getProdName());
            skuStocksMap.put(sku.getSkuId(), mapSku);
        }
        return sku;
    }


}
